package com.edou.cms.controller.admin;

import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import com.edou.base.controller.BaseController;
import com.edou.base.util.UUIDUtil;
import com.edou.cms.entity.CmsAdv;
import com.edou.cms.entity.CmsAdvPhoto;
import com.edou.cms.form.CmsAdvPhotoForm;
import com.edou.cms.service.ICmsAdvPhotoService;
import com.edou.cms.service.ICmsAdvService;

/**
 * 
 * 广告管理
 * @author 方泉
 *
 */
@Controller("advPhotoAdminController")
@Scope("prototype")
@RequestMapping(value = "/admin/cms/advPhoto")
public class AdvPhotoAdminController extends BaseController{

	
	@Resource
	private ICmsAdvPhotoService cmsAdvPhotoService;
	@Resource
	private ICmsAdvService cmsAdvService;
	
	@RequestMapping(value = "/list.htm", method = RequestMethod.POST)
	public ModelAndView list(CmsAdvPhotoForm model) {
		ModelAndView view = new ModelAndView();
		view.setViewName("/admin/cms/adv_photo_list");
		Map<String,Object> data = view.getModel();
		// 设置当前页
		if(model.getPage() != null){
			model.getPageResult().setCurrentPage(model.getPage());
		}
		CmsAdv cmsAdv =cmsAdvService.getAdvById(model.getAdvId());
		model.setCmsAdv(cmsAdv);
		// 根据广告位ID分页查询图片
		Integer maxCount = cmsAdv.getMaxPhotoNum()==null?100:cmsAdv.getMaxPhotoNum();
		model.getPageResult().setPageSize(maxCount);
		cmsAdvPhotoService.listAdvPhotoByPage(model.getPageResult(), model.getAdvId());
		data.put("pageResult", model.getPageResult());
		data.put("cmsAdv", model.getCmsAdv());
		data.put("advId", model.getAdvId());
		data.put("page", model.getPage());
		return view;
	}

	@RequestMapping(value = "/to_add.htm", method = RequestMethod.GET)
	public ModelAndView toAdd(CmsAdvPhotoForm model) {
		ModelAndView view = new ModelAndView();
		view.setViewName("/admin/cms/adv_photo_add");
		Map<String,Object> data = view.getModel();
		data.put("item", model.getItem());
		return view;
	}
	
	@RequestMapping(value = "/add.htm", method = RequestMethod.POST)
	public void add(@ModelAttribute CmsAdvPhotoForm cmsAdvPhotoForm,HttpServletRequest request,HttpServletResponse response) {
		try{
			CmsAdvPhoto photo = cmsAdvPhotoForm.getItem();
			
			// 设立图片的ID
			photo.setId(UUIDUtil.uuid());
			
			// 如果图片名称为空时，将文件名默认为图片名称
			if(!StringUtils.isNotBlank(photo.getName())){
				photo.setName(cmsAdvPhotoForm.getItem().getFileName());
			}
			
			// 如果排序为空时，默认排序为1
			if(null == photo.getSort()){
				photo.setSort(1);
			}
			
			cmsAdvPhotoService.addAdvPhoto(photo);
			ajaxJSON(Status.success);
		}catch(Exception ex){
			String msg = "新增广告图时发生异常："+ex.getMessage();
			logger.error(msg,ex);
			ajaxJSON(Status.error, "新增广告图失败");
		}
	}
	
	@RequestMapping(value = "/to_update.htm", method = RequestMethod.GET)
	public ModelAndView to_update(CmsAdvPhotoForm model) {
		ModelAndView view = new ModelAndView();
		view.setViewName("/admin/cms/adv_photo_add");
		Map<String,Object> data = view.getModel();
		String photoId = model.getItem().getId();
		CmsAdvPhoto photo = cmsAdvPhotoService.getAdvPhotoById(photoId);
		data.put("item", photo);
		return view;
	}
	

	
	/**
	 * 修改广告图
	 * @return
	 */
	@RequestMapping(value = "/update.htm", method = RequestMethod.POST)
	public void update(@ModelAttribute CmsAdvPhotoForm cmsAdvPhotoForm,HttpServletRequest request,HttpServletResponse response) {
		try{
			CmsAdvPhoto photo = cmsAdvPhotoForm.getItem();
			
			// 如果图片名称为空时，将文件名默认为图片名称
			if(!StringUtils.isNotBlank(photo.getName())){
				photo.setName(cmsAdvPhotoForm.getItem().getFileName());
			}
			
			// 如果排序为空时，默认排序为1
			if(null == photo.getSort()){
				photo.setSort(1);
			}
			
			cmsAdvPhotoService.updateAdvPhoto(photo);
			
			ajaxJSON(Status.success);
		}catch(Exception ex){
			String msg = "修改广告图时发生异常："+ex.getMessage();
			logger.error(msg,ex);
			ajaxJSON(Status.error, "修改广告图失败");
		}
	}
	
	/**
	 * 删除广告图
	 * @return
	 */
	@RequestMapping(value = "/remove.htm", method = RequestMethod.POST)
	public void remove(@ModelAttribute CmsAdvPhotoForm cmsAdvPhotoForm,HttpServletRequest request,HttpServletResponse response) {
		try{
			String id = cmsAdvPhotoForm.getItem().getId();
			cmsAdvPhotoService.removeAdvPhotoById(id);
			
			ajaxJSON(Status.success);
		}catch(Exception ex){
			String msg = "删除广告图时发生异常："+ex.getMessage();
			logger.error(msg,ex);
			ajaxJSON(Status.error, "删除广告图失败");
		}
	}
	
}
